package top.lyunk.ddd.common.core;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import top.lyunk.ddd.common.exception.BaseErrorCodeEnum;

import java.io.Serializable;

/**
 * <pre>
 * 说明：
 * </pre>
 *
 * @author lyk
 * @date 2022/9/2
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class JsonResult<T extends Serializable> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 请求成功
     */
    private static final boolean REQUEST_SUCCESS = true;

    /**
     * 请求失败
     */
    private static final boolean REQUEST_FAIL = false;

    /**
     * 请求结果
     */
    private boolean success;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 错误码
     */
    private String errCode;
    /**
     * 错误信息
     */
    private String errMsg;

    public static <T extends Serializable> JsonResult<T> buildSuccess(){
        return new JsonResult<>(REQUEST_SUCCESS, null, null, null);
    }

    public static <T extends Serializable> JsonResult<T> buildSuccess(T data){
        return new JsonResult<>(REQUEST_SUCCESS, data, null, null);
    }

    /**
     * 失败，枚举类定义错误码和信息
     */
    public static <T extends Serializable> JsonResult<T> buildError(BaseErrorCodeEnum baseErrorCodeEnum) {
        return new JsonResult<>(REQUEST_FAIL, null, baseErrorCodeEnum.getErrorCode(), baseErrorCodeEnum.getErrorMsg());
    }
}
